//
//  LZStringExtension.swift
//  halikaPeso
//
//  Created by L-zh on 2024/12/16.
//

import UIKit
import CommonCrypto

extension String {
    public var md5String:String  {
        let str = self.cString(using: String.Encoding.utf8)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(CC_MD5_DIGEST_LENGTH))
        CC_MD5(str!, strLen, result)
        let hash = NSMutableString()
        for i in 0 ..< digestLen {
            hash.appendFormat("%02x", result[i])
        }
        free(result)
        return String(format: hash as String).uppercased()
    }
    
    
    subscript (i: Int) -> String {
        return self[i ..< i + 1]
    }
    
    func substring(fromIndex: Int) -> String {
        return self[min(fromIndex, self.count) ..< self.count]
    }
    
    func substring(toIndex: Int) -> String {
        return self[0 ..< max(0, toIndex)]
    }
    
    subscript (r: Range<Int>) -> String {
        let range = Range(uncheckedBounds: (lower: max(0, min(self.count, r.lowerBound)), upper: min(self.count, max(0, r.upperBound))))
        let start = index(startIndex, offsetBy: range.lowerBound)
        let end = index(start, offsetBy: range.upperBound - range.lowerBound)
        return String(self[start ..< end])
    }
    
    
    /// 金额用逗号分割
//    public var decimalStr:String? {
//       
//       let tempStrList = components(separatedBy: ".")
//        //初始化NumberFormatter
//        let format = NumberFormatter()
//        //设置numberStyle（有多种格式）
//        format.numberStyle = .decimal
//        //转换后的string
//        guard let string = format.string(from: NSNumber(value:Float(tempStrList.first ?? "0") ?? 0 )) else { return "" }
//        //打印
//        return string + (tempStrList.count > 1 ? "." + (tempStrList.last ?? "")  : "")
//    }
    
    /// 日历时间起止时间和结束时间拼接
    ///
    /// - Parameters:
    ///   - startDate: 开始时间
    ///   - endDate: 结束时间
    /// - Returns: 拼接字符串
//    public static func formatterDateStr(startDate:String?,endDate:String?) -> String {
//        var formatterStr = startDate ?? ""
//        if endDate == nil {
//           return formatterStr  + "  "
//        }
//        formatterStr = formatterStr + " 至 " + (endDate ?? "") + "  "
//        return formatterStr
//    }
}




